home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / MacGofer 0.22d / MacGofer Sources / mac_undo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-06  |  2.8 KB  |  139 lines  |  [TEXT/MPS ]

  1. /*****************************************************************************
  2.  
  3.   mac_undo.c:  Copyright (c) Kevin Hammond 1993.   All rights reserved.
  4.   
  5.   This module handles text undo.
  6.   
  7. *****************************************************************************/
  8.  
  9. #include "mac.h"
  10.  
  11. extern char *safemalloc();
  12.  
  13. int undolen, undostart;
  14. char *undobuff = NIL;
  15.  
  16. int keystart = -1;
  17.  
  18. Boolean    pasted = FALSE;        /* Set after a paste */
  19.  
  20. extern Boolean copyscrap;
  21.  
  22. saveundo(windex)
  23. int windex;
  24. {
  25.   TEHandle teh = TEHANDLE(windex);
  26.   char **chs;
  27.  
  28.   if(undobuff != NIL)
  29.     free(undobuff);
  30.   
  31.   TEHLock(windex);
  32.   chs = (char **) (*teh)->hText;
  33.   HLock((Handle)chs);
  34.   undostart = (*teh)->selStart;
  35.   undolen =   (*teh)->selEnd-undostart;
  36.   
  37.   /* malloc(0) probably won't work. */
  38.   if(undolen > 0)
  39.     {
  40.       undobuff = safemalloc(undolen);
  41.       strncpy(undobuff,*chs+undostart,undolen);
  42.     }
  43.   else
  44.     undobuff = safemalloc(1);
  45.  
  46.   keystart = -1;
  47.   pasted = FALSE;
  48.   HUnlock((Handle)chs);
  49.   TEHUnlock(windex);
  50.  
  51.   setitem(Menu_Edit,MItem_Undo,"Undo");
  52.   EnableItem(Menu_Edit,MItem_Undo);
  53. }
  54.  
  55.  
  56. cantundo()
  57. {
  58.   if(undobuff != NIL)
  59.     {
  60.       free(undobuff);
  61.       undobuff = NIL;
  62.       setitem(Menu_Edit,MItem_Undo,"Can't Undo");
  63.       DisableItem(Menu_Edit,MItem_Undo);
  64.       keystart = -1;
  65.       pasted = FALSE;
  66.     }
  67. }
  68.  
  69.  
  70. undo(windex)
  71. int windex;
  72. {
  73.   if(isLegalWindow(windex) && undobuff != NIL)
  74.     {
  75.       TEHandle teh = TEHANDLE(windex);
  76.       int tempundolen, tempundostart;
  77.       char *tempundobuff;
  78.       
  79.       TEHLock(windex);
  80.       tempundostart = keystart < 0 ? (*teh)->selStart : keystart;
  81.       tempundolen =   (*teh)->selEnd-tempundostart;
  82.       
  83.       /* Is there enough memory? */
  84.       CheckMemory("Undo",undolen+tempundolen+512);
  85.  
  86.       /* malloc(0) probably won't work. */
  87.       if(tempundolen > 0)
  88.         {
  89.       tempundobuff = safemalloc(tempundolen);
  90.           strncpy(tempundobuff,*((*teh)->hText)+tempundostart,tempundolen);
  91.         }
  92.       else
  93.         tempundobuff = safemalloc(1);
  94.       
  95.       TESetSelect(tempundostart,tempundostart+tempundolen,teh);
  96.       if(windex == worksheet)
  97.     AdjustPromptCount(TRUE);
  98.  
  99.       if(tempundolen > 0)
  100.         TEDelete(teh);
  101.       if(undolen > 0)
  102.         TEInsert(undobuff,undolen,teh);
  103.  
  104.       TESetSelect(tempundostart,tempundostart+undolen,teh);
  105.       if(windex == worksheet)
  106.     AdjustPromptCount(FALSE);
  107.  
  108.       free(undobuff);
  109.       undostart = tempundostart;
  110.       undobuff = tempundobuff;
  111.       undolen =  tempundolen;
  112.       TEHUnlock(windex);
  113.       
  114.       copyscrap = FALSE;
  115.       pasted = FALSE;
  116.       keystart = -1;
  117.     }
  118. }
  119.  
  120.  
  121. pasteundo(windex)
  122. int windex;
  123. {
  124.   saveundo(windex);
  125.   keystart = (*TEHANDLE(windex))->selStart;
  126.   pasted = TRUE;
  127. }
  128.  
  129.  
  130. keyundo(windex)
  131. int windex;
  132. {
  133.   if(keystart < 0 || pasted)
  134.     {
  135.       saveundo(windex);
  136.       keystart = (*TEHANDLE(windex))->selStart;
  137.     }
  138. }
  139.